package ServletContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
/*
    配置文件(也就是资源文件): 存放的地方有三个位置
                    1: src
                    2: web
                    3: WEB-INF


3: 获取文件的真实(服务器)路径
                         1 : 方法 : String getRealPath(String path)
*/

@WebServlet("/servletContextDemo03")
public class ServletContextDemo03 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //通过HttpServlet对象获取ServletContext
        ServletContext aa = this.getServletContext();
        //获取文件的服务器路径
        String bb = aa.getRealPath("/b.txt"); //Web目录下的资源访问
        System.out.println(bb);
        //File cc=new File(bb);

        String dd = aa.getRealPath("/WEB-INF/c.txt");//WEN-INF目录下的资源访问
        System.out.println(dd);

        String ee = aa.getRealPath("/WEB-INF/classes/a.txt");//src下的资源文件访问
        System.out.println(ee);


    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
          this.doPost(req,resp);
    }
}
